var ci = {
  addAttribute : function(attrName, attrValue){
    var attributeChilds = document.getElementById('attributeChilds');
    var item = document.createElement('treeitem');
    var row = document.createElement('treerow');
    var c1 = document.createElement('treecell');
    var c2 = document.createElement('treecell');
    c1.setAttribute('label', attrName);
    c2.setAttribute('label', attrValue);
    row.appendChild(c1);
    row.appendChild(c2);
    item.appendChild(row);
    attributeChilds.appendChild(item);
  },

  removeSelectedAttribute : function(){
    var attributeTree = document.getElementById('attributeTree');
    var attributeChilds = document.getElementById('attributeChilds');
    //alert(attributeTree.currentIndex);
    var childNodes = attributeChilds.childNodes;
    attributeChilds.removeChild(childNodes[attributeTree.currentIndex]);
  },

  inputAttribute : function(){
  	var retVals = { attrName : null, attrValue : null};
		openDialog("attributeDialog.xul", "dlg-attribute", "modal,close=no", retVals);
		if(retVals.attrName != null){
			ci.addAttribute(retVals.attrName,retVals.attrValue);
		}
  },

  init : function(){
	var currentUri = getURL() + "";
	/*if(currentUri == "" || (currentUri.substring(0, "about:".length) == "about:")){
	    alert("Please open a web page first!");
	    return;
	}*/
	var URIs = parseUri(currentUri);
	document.getElementById('txtCorpus').value = URIs.directory;
	document.getElementById('txtName').value = URIs.file;
  },

  doCancel : function(){
  	return true;
  },

  doOK : function(){
  	var stringBundle = document.getElementById("sbLasuli");
  	var pref = new Preferences(PREF_PREFIX);
  	var defaultService = pref.getPref("service", DEFAULT_SERVICE);
  	var user = pref.getPref("username", DEFAULT_HT_USER);
  	var pass = pref.getPref("password", DEFAULT_HT_PASS);
  	var defaultWebDav = pref.getPref("webdav", DEFAULT_SERVICE);
  	//var wdUser = pref.getPref("webdav_user", DEFAULT_HT_USER);
  	//var wdPass = pref.getPref("webdav_pass", DEFAULT_HT_PASS);

  	var wdCorpus = document.getElementById('txtCorpus').value;
  	var wdSource = document.getElementById('txtName').value;

  	if('undefined' == typeof wdSource || wdSource == ""){
    	alert(stringBundle.getString("createItem.error.sourcenameisnull"));
    	return false;
  	}
		if(wdCorpus.startsWith("/")) wdCorpus = wdCorpus.substr(1);
  	if(wdCorpus.endsWith("/")) wdCorpus = wdCorpus.substring(0,wdCorpus.length-1);

  	var dav = new WebDav(wdCorpus, wdSource);
		debug("[createItem.js:doOK] wdCorpus:" + wdCorpus + ", wdSource:" + wdSource, 3);
		var davUrl = defaultWebDav + wdCorpus + "/" + wdSource;
		debug("[createItem.js:doOK] davUrl:" + davUrl, 3);
		//var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
    //       getService(Components.interfaces.nsIWindowMediator);
  	//var recentWindow = wm.getMostRecentWindow("navigator:browser");

  	//var docHtml = recentWindow.content.document.innerHTML;
		var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].
           getService(Components.interfaces.nsIWindowMediator);
  	var recentWindow = wm.getMostRecentWindow("navigator:browser");

  	// TODO: innerHTML will return <head>and<body> tag!
  	var docHtml = recentWindow ? recentWindow.content.document.documentElement.textContent : null;
  	//debug("[createItem.js:doOK] url:" + url, 3);
  	//var docHtml = opener.content.document.documentElement.innerHTML
  	debug("[createItem.js:doOK] docHtml:" + docHtml, 3);
    if(docHtml == null)
    {
    	alert(stringBundle.getString("createItem.error.documentisnull"));
    	return false;
    }
    var res = dav.save(docHtml);
    if(!res){
    	alert(stringBundle.getString("createItem.error.webdaverror") + "\nUrl:" + davUrl);
    	return false;
    }

    if(wdSource.indexOf(".") > 0) wdSource = wdSource.substring(0,wdSource.indexOf("."));
    var entityUrl = defaultService + "entity/" + wdCorpus + "/" + wdSource;
    var oEntity = new Entity();
    oEntity.uri = entityUrl;
    debug("[createItem.js:doOK] entityUrl: " + entityUrl, 3);
    oEntity.addResource("source",davUrl);
    var attributeChilds = document.getElementById('attributeChilds');
  	var rows = attributeChilds.childNodes;
  	for(var i=0; i < rows.length; i++){
  		var cells = rows[i].firstChild.childNodes;
  		oEntity.addAttribute(cells[0].getAttribute('label'),cells[1].getAttribute('label'));
  	}
  	debug("[createItem.js:doOK] entity xml: " + oEntity.toXml(), 4);
    var res = oEntity.httpPut();
    if(res == null){
    	alert(stringBundle.getString("createItem.error.hterror"));
	  	return false;
		}else
			alert(stringBundle.getString("createItem.ok"));
  }
}